package com.xiaoyy.core.config.set;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程池配置类
 * @author chenzh
 * @date 2018-10-11
 * **/
@Configuration
public class ExecutorConfig {

    @Bean(name="oplogTaskExecutor")
    public ThreadPoolTaskExecutor requestLogTaskExecutor(){
        ThreadPoolTaskExecutor poolTaskExecutor = new ThreadPoolTaskExecutor();
        //线程池维护线程的最少数量
        poolTaskExecutor.setCorePoolSize(1);
        //线程池维护线程的最大数量
        poolTaskExecutor.setMaxPoolSize(10);
        // 设置默认线程名称
        poolTaskExecutor.setThreadNamePrefix("saveOplog-");
        // 线程池对拒绝任务(无线程可用)的处理策略 ThreadPoolExecutor.CallerRunsPolicy策略 ,调用者的线程会执行该任务,如果执行器已关闭,则丢弃.
        poolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //线程池所使用的缓冲队列
        poolTaskExecutor.setQueueCapacity(20);
        //线程池维护线程所允许的空闲时间 单位秒
        poolTaskExecutor.setKeepAliveSeconds(20);
        // 等待所有任务结束后再关闭线程池
        poolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        poolTaskExecutor.initialize();
        return poolTaskExecutor;
    }

    @Bean(name="apiLogTaskExecutor")
    public ThreadPoolTaskExecutor apiLogTaskExecutor(){
        ThreadPoolTaskExecutor poolTaskExecutor = new ThreadPoolTaskExecutor();
        //线程池维护线程的最少数量
        poolTaskExecutor.setCorePoolSize(1);
        //线程池维护线程的最大数量
        poolTaskExecutor.setMaxPoolSize(10);
        // 设置默认线程名称
        poolTaskExecutor.setThreadNamePrefix("saveApiLog-");
        // 线程池对拒绝任务(无线程可用)的处理策略 ThreadPoolExecutor.CallerRunsPolicy策略 ,调用者的线程会执行该任务,如果执行器已关闭,则丢弃.
        poolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //线程池所使用的缓冲队列
        poolTaskExecutor.setQueueCapacity(20);
        //线程池维护线程所允许的空闲时间 单位秒
        poolTaskExecutor.setKeepAliveSeconds(20);
        // 等待所有任务结束后再关闭线程池
        poolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        poolTaskExecutor.initialize();
        return poolTaskExecutor;
    }

    @Bean(name="quartzTaskExecutor")
    public ThreadPoolTaskExecutor atgLogTaskExecutor(){
        ThreadPoolTaskExecutor poolTaskExecutor = new ThreadPoolTaskExecutor();
        //线程池维护线程的最少数量
        poolTaskExecutor.setCorePoolSize(1);
        //线程池维护线程的最大数量
        poolTaskExecutor.setMaxPoolSize(10);
        // 设置默认线程名称
        poolTaskExecutor.setThreadNamePrefix("quartzTask-");
        // 线程池对拒绝任务(无线程可用)的处理策略 ThreadPoolExecutor.CallerRunsPolicy策略 ,调用者的线程会执行该任务,如果执行器已关闭,则丢弃.
        poolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //线程池所使用的缓冲队列
        poolTaskExecutor.setQueueCapacity(20);
        //线程池维护线程所允许的空闲时间 单位秒
        poolTaskExecutor.setKeepAliveSeconds(20);
        // 等待所有任务结束后再关闭线程池
        poolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        poolTaskExecutor.initialize();
        return poolTaskExecutor;
    }
    /**
     * api转换线程池
     * */
    @Bean(name="apiConvertExecutor")
    public ThreadPoolTaskExecutor apiConvertExecutor(){
        ThreadPoolTaskExecutor poolTaskExecutor = new ThreadPoolTaskExecutor();
        //线程池维护线程的最少数量
        poolTaskExecutor.setCorePoolSize(10);
        //线程池维护线程的最大数量
        poolTaskExecutor.setMaxPoolSize(100);
        // 设置默认线程名称
        poolTaskExecutor.setThreadNamePrefix("apiConvert-");
        // 线程池对拒绝任务(无线程可用)的处理策略 ThreadPoolExecutor.CallerRunsPolicy策略 ,调用者的线程会执行该任务,如果执行器已关闭,则丢弃.
        poolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //线程池所使用的缓冲队列
        poolTaskExecutor.setQueueCapacity(50);
        //线程池维护线程所允许的空闲时间 单位秒
        poolTaskExecutor.setKeepAliveSeconds(10);
        // 等待所有任务结束后再关闭线程池
        poolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        poolTaskExecutor.initialize();
        return poolTaskExecutor;
    }
    /**
     * api异步发送线程池
     * */
    @Bean(name="apiAsynSend")
    public ThreadPoolTaskExecutor apiAsynSend(){
        ThreadPoolTaskExecutor poolTaskExecutor = new ThreadPoolTaskExecutor();
        //线程池维护线程的最少数量
        poolTaskExecutor.setCorePoolSize(3);
        //线程池维护线程的最大数量
        poolTaskExecutor.setMaxPoolSize(20);
        // 设置默认线程名称
        poolTaskExecutor.setThreadNamePrefix("apiAsynSend-");
        // 线程池对拒绝任务(无线程可用)的处理策略 ThreadPoolExecutor.CallerRunsPolicy策略 ,调用者的线程会执行该任务,如果执行器已关闭,则丢弃.
        poolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //线程池所使用的缓冲队列
        poolTaskExecutor.setQueueCapacity(50);
        //线程池维护线程所允许的空闲时间 单位秒
        poolTaskExecutor.setKeepAliveSeconds(20);
        // 等待所有任务结束后再关闭线程池
        poolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        poolTaskExecutor.initialize();
        return poolTaskExecutor;
    }
    /**
     * api定时发送线程池
     * */
    @Bean(name="apiTimingSend")
    public ThreadPoolTaskExecutor apiTimingSend(){
        ThreadPoolTaskExecutor poolTaskExecutor = new ThreadPoolTaskExecutor();
        //线程池维护线程的最少数量
        poolTaskExecutor.setCorePoolSize(3);
        //线程池维护线程的最大数量
        poolTaskExecutor.setMaxPoolSize(50);
        // 设置默认线程名称
        poolTaskExecutor.setThreadNamePrefix("apiTimingSend-");
        // 线程池对拒绝任务(无线程可用)的处理策略 ThreadPoolExecutor.CallerRunsPolicy策略 ,调用者的线程会执行该任务,如果执行器已关闭,则丢弃.
        poolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //线程池所使用的缓冲队列
        poolTaskExecutor.setQueueCapacity(50);
        //线程池维护线程所允许的空闲时间 单位秒
        poolTaskExecutor.setKeepAliveSeconds(20);
        // 等待所有任务结束后再关闭线程池
        poolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        poolTaskExecutor.initialize();
        return poolTaskExecutor;
    }
}

